package com.team7.sso.service.impl;

import com.alibaba.fastjson.JSON;
import com.team7.sso.entity.User;
import com.team7.sso.feign.UserFeign;
import com.team7.sso.service.UserService;
import com.team7.sso.vo.CommonResult;
import com.team7.sso.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserFeign userFeign;

    @Override
    public CommonResult loginByUserNameAndPassword(LoginVo loginVo) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginVo.getUsername(), loginVo.getPassword());
        try{
            System.out.println(token+"00000000");
            subject.login(token); //调用自己的realm中认证方法
            User user = (User) subject.getPrincipal();
            String key = UUID.randomUUID().toString().replace("-", ""); //token

            redisTemplate.opsForValue().set(key,JSON.toJSONString(user),24, TimeUnit.HOURS);
            //服务与服务之间调用 就直接返回java对象
            //如果时vue前端调用后端接口  则返回CommonResult
            List<String> permissions = userFeign.findUserPermissionById(user.getId());

            Map<String,Object> result=new HashMap<>();
            result.put("token",key);
            result.put("permissions",permissions);
            return new CommonResult(2000, "登陆成功", result);

        } catch (AuthenticationException e) {
            log.error(e.toString()); //保存到文件中 sout(只能输出到控制台)
            return new CommonResult(5000,"密码错误或账号已禁用",null);
        }
    }

    @Override
    public Map<String, String> updateUserPassword(String oldPassword, String newPassword, String salt) {
        Md5Hash md5Hash = new Md5Hash(oldPassword, salt, 1024);//旧密码

        //新密码
        String salt2=UUID.randomUUID().toString().replace("-","").substring(1,5);
        Md5Hash md5Hash2 = new Md5Hash(newPassword, salt2, 1024);
        Map<String, String> map = new HashMap<>();
        map.put("oldPassword",md5Hash.toString());
        map.put("newPassword",md5Hash2.toString());
        map.put("newSalt",salt2);
        return map;
    }

    @Override
    public Map<String, String> getPassword(String password, String salt) {
        Md5Hash md5Hash2 = new Md5Hash(password, salt, 1024);
        Map<String, String> map = new HashMap<>();
        map.put("password",md5Hash2.toString());
        return map;
    }


}
